<template>
  <div class="wip-manage">
    <el-card shadow="hover">
      <div class="wip-header">
        <el-input
          v-model="searchText"
          placeholder="搜索物料名称/编码"
          clearable
          style="width:200px; margin-right:16px;"
          @keyup.enter="doSearch"
        />
        <el-select v-model="searchStatus" placeholder="状态" clearable style="width:120px; margin-right:16px;">
          <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item"/>
        </el-select>
        <el-button type="primary" @click="handleAdd">新增在制品</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="id" label="在制品编号" width="120px"/>
        <el-table-column prop="materialName" label="物料名称" width="140px"/>
        <el-table-column prop="materialCode" label="物料编码" width="120px"/>
        <el-table-column prop="quantity" label="数量" width="100px"/>
        <el-table-column prop="station" label="当前工序" width="120px"/>
        <el-table-column prop="status" label="状态" width="100px">
          <template #default="scope">
            <el-tag :type="statusTagType(scope.row.status)">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="arriveTime" label="到站时间" width="160px"/>
        <el-table-column prop="manager" label="负责人" width="100px"/>
        <el-table-column prop="remark" label="备注" width="140px"/>
        <el-table-column label="操作" width="160px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="440px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="90px">
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="editRow.materialName"/>
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="editRow.materialCode"/>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input-number v-model="editRow.quantity" :min="1" style="width:100%;"/>
        </el-form-item>
        <el-form-item label="当前工序" prop="station">
          <el-select v-model="editRow.station" placeholder="请选择">
            <el-option v-for="item in stationOptions" :key="item" :label="item" :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editRow.status" placeholder="请选择">
            <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="到站时间" prop="arriveTime">
          <el-date-picker v-model="editRow.arriveTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width:100%;" />
        </el-form-item>
        <el-form-item label="负责人" prop="manager">
          <el-input v-model="editRow.manager"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editRow.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该在制品吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

interface WipItem {
  id: string
  materialName: string
  materialCode: string
  quantity: number
  station: string
  status: string
  arriveTime: string
  manager: string
  remark: string
}

// 状态与工序
const statusOptions = ['生产中', '待检', '暂停', '已完成']
const stationOptions = ['钢筋绑扎', '混凝土浇筑', '玻璃组装', '电线敷设', '防水喷涂']

// 10条模拟数据
const wipList = ref<WipItem[]>([
  { id: 'W001', materialName: 'HRB400钢筋', materialCode: 'M001', quantity: 20, station: '钢筋绑扎', status: '生产中', arriveTime: '2024-06-01 08:30:00', manager: '张三', remark: '' },
  { id: 'W002', materialName: 'PO42.5水泥', materialCode: 'M002', quantity: 50, station: '混凝土浇筑', status: '待检', arriveTime: '2024-06-01 09:00:00', manager: '李四', remark: '' },
  { id: 'W003', materialName: '钢化玻璃', materialCode: 'M003', quantity: 10, station: '玻璃组装', status: '生产中', arriveTime: '2024-06-01 09:45:00', manager: '王五', remark: '' },
  { id: 'W004', materialName: '石膏板', materialCode: 'M004', quantity: 30, station: '玻璃组装', status: '暂停', arriveTime: '2024-06-01 10:15:00', manager: '张三', remark: '' },
  { id: 'W005', materialName: 'BV铜线', materialCode: 'M005', quantity: 100, station: '电线敷设', status: '生产中', arriveTime: '2024-06-01 10:40:00', manager: '李四', remark: '' },
  { id: 'W006', materialName: '防水涂料', materialCode: 'M006', quantity: 15, station: '防水喷涂', status: '待检', arriveTime: '2024-06-01 11:20:00', manager: '王五', remark: '' },
  { id: 'W007', materialName: 'HRB400钢筋', materialCode: 'M001', quantity: 25, station: '钢筋绑扎', status: '生产中', arriveTime: '2024-06-01 13:00:00', manager: '张三', remark: '' },
  { id: 'W008', materialName: '钢化玻璃', materialCode: 'M003', quantity: 8, station: '玻璃组装', status: '生产中', arriveTime: '2024-06-01 13:30:00', manager: '李四', remark: '' },
  { id: 'W009', materialName: 'BV铜线', materialCode: 'M005', quantity: 60, station: '电线敷设', status: '暂停', arriveTime: '2024-06-01 14:00:00', manager: '王五', remark: '' },
  { id: 'W010', materialName: 'PO42.5水泥', materialCode: 'M002', quantity: 40, station: '混凝土浇筑', status: '生产中', arriveTime: '2024-06-01 15:00:00', manager: '张三', remark: '' }
])

// 搜索
const searchText = ref('')
const searchStatus = ref('')
const filteredList = computed(() => {
  let arr = wipList.value
  if (searchText.value) {
    arr = arr.filter(
      r =>
        r.materialName.includes(searchText.value) ||
        r.materialCode.includes(searchText.value)
    )
  }
  if (searchStatus.value) {
    arr = arr.filter(r => r.status === searchStatus.value)
  }
  return arr
})
function doSearch() {}

// 新增/编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增在制品')
const editRow = reactive<WipItem>({
  id: '',
  materialName: '',
  materialCode: '',
  quantity: 1,
  station: '',
  status: '',
  arriveTime: '',
  manager: '',
  remark: ''
})
const formRef = ref()
const formRules = {
  materialName: [{ required: true, message: '必填', trigger: 'blur' }],
  materialCode: [{ required: true, message: '必填', trigger: 'blur' }],
  quantity: [{ required: true, message: '必填', trigger: 'blur' }],
  station: [{ required: true, message: '必选', trigger: 'change' }],
  status: [{ required: true, message: '必选', trigger: 'change' }],
  arriveTime: [{ required: true, message: '必填', trigger: 'change' }],
  manager: [{ required: true, message: '必填', trigger: 'blur' }]
}

function handleAdd() {
  dialogTitle.value = '新增在制品'
  Object.assign(editRow, { id: '', materialName: '', materialCode: '', quantity: 1, station: '', status: '', arriveTime: '', manager: '', remark: '' })
  dialogVisible.value = true
}
function handleEdit(row: WipItem) {
  dialogTitle.value = '编辑在制品'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = wipList.value.findIndex(t => t.id === editRow.id)
      if (idx !== -1) wipList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      // 自动生成新编号
      editRow.id = 'W' + (wipList.value.length + 1).toString().padStart(3, '0')
      wipList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}

const statusTagType = (status: string | undefined): 'success' | 'warning' | 'danger' | 'info' | 'primary' | undefined => {
  switch (status) {
    case '生产中': return 'success'
    case '待检': return 'warning'
    case '暂停': return 'danger'
    case '已完成': return 'info'
    default: return 'primary'
  }
}

// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<WipItem | null>(null)
function handleDelete(row: WipItem) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    wipList.value = wipList.value.filter(t => t.id !== deleteTarget.value!.id)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.wip-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.wip-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
